 /**
 * Description: 物资管理
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.biz.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.github.pagehelper.PageInfo;
import com.metro.core.annotation.BusinessDesc;
import com.metro.core.page.Page;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.QueryParams;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.paperless.biz.beans.vo.BizMaterialsManage;
import com.metro.paperless.biz.service.inf.IBizMaterialsManageService;
import com.metro.paperless.common.beans.constant.DicTypeEnum;
import com.metro.paperless.common.utils.DicUtil;
import com.metro.paperless.sys.beans.vo.SysMaterialsType;
import com.metro.paperless.sys.service.inf.ISysMaterialsTypeService;
import com.metro.paperless.web.controller.BaseController;
 /**
 * 物资管理<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Controller
@RequestMapping("/bizMaterialsManage")
public class BizMaterialsManageController extends BaseController {
    //菜单编码
    private static final String menuCode = "BIZ_MATERIALS_MANAGE";
    //模块描述
    private static final String MODULE_DESC = "物资管理";
    @Autowired
    private IBizMaterialsManageService bizmaterialsmanageService;
    @Autowired
    private ISysMaterialsTypeService materialsTypeService;
    @Override
    protected String getViewDir() {
        return "biz/";
    }
    
    /**
     * 去查询物资管理
     * 
     * @param bizmaterialsmanage 物资管理页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询物资管理列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "list", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView list(BizMaterialsManage bizmaterialsmanage) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.setViewName(getView("bizmaterialsmanage_list"));
            mav.addObject("materialsTypes", DicUtil.getLabelValueList(DicTypeEnum.MAT_TYPE_HIDDEN));
            mav.addObject("searchEntity", bizmaterialsmanage);
        } catch (Exception ex) {
            log.error("查询物资管理出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询物资管理
     *
     * @param bizmaterialsmanage 物资管理页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询物资管理列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolist", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolist(BizMaterialsManage bizmaterialsmanage, Page page, QueryParams<BizMaterialsManage> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setPaging(page);
            queryParams.setEntity(bizmaterialsmanage);
            PageInfo<BizMaterialsManage> pageInfo = bizmaterialsmanageService.queryForPage(queryParams);
            jr.setData(pageInfo);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询物资管理出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 去新增物资管理
     * 
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去新增物资管理")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "create")
    public ModelAndView createForm(Model model, HttpServletRequest request,String materialsType){
        ModelAndView mav = new ModelAndView();
        try {
            initCreateOrUpdateFrom(mav, "create");
        } catch (Exception ex) {
            log.error("去新增物资管理出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }
    
    /**
     * 初始化新增修改物资管理所需参数
     * @author Hunter
     */
    private void initCreateOrUpdateFrom(ModelAndView mav, String action){
    	DicUtil.reloadDataInDicTypeEnumPar(DicTypeEnum.MAT_TYPE_HIDDEN);
    	DicUtil.reloadDataInDicTypeEnumPar(DicTypeEnum.MAT_UNIT_HIDDEN);
        mav.addObject("curStatus", DicUtil.getLabelValueList(DicTypeEnum.CUR_STATUS));
        mav.addObject("materialsTypes", DicUtil.getLabelValueList(DicTypeEnum.MAT_TYPE_HIDDEN));
        mav.addObject("units", DicUtil.getLabelValueList(DicTypeEnum.MAT_UNIT_HIDDEN));
        mav.addObject("action", action);
        mav.setViewName(getView("bizmaterialsmanage_form"));
    }
    
    /**
     * 物资列表
     * @return
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查看物资借还记录")
    @RequestMapping(value="select", method = {RequestMethod.GET})
    public ModelAndView createSelectForm(){
    	ModelAndView mav= new ModelAndView();
    	List<SysMaterialsType> materialsTypeList = materialsTypeService.queryForList(new SysMaterialsType());
    	try{
    		mav.setViewName(getView("appmaterials_select"));
    		mav.addObject("materialsTypeList", materialsTypeList);
    	}catch(Exception ex){
    		log.error("去新增物资管理出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
    	}
    	return mav;
    }
    
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询物资管理列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolistAll", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolistAll(BizMaterialsManage bizmaterialsmanage) {
        JsonResult jr = new JsonResult();
        try {
            List<BizMaterialsManage> list = bizmaterialsmanageService.queryForList(bizmaterialsmanage);
            jr.setData(list);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询物资管理出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    
    
    /**
     * 新增物资管理
     * 
     * @param entity 物资管理页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增物资管理")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveBizMaterialsManage(BizMaterialsManage entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
      //判断物资来源是否存在
        IResult result = bizmaterialsmanageService.checkMaterials(entity);
    	String code = result.getCode();
    	 if ( code.equals(ResultCode.DUPLICATE_KEY.getCode())) {
        	 jr.setResult(result);
             log.debug("内部单位已存在【{}】", entity);
    	 }else {
		        try {
		            IResult res = bizmaterialsmanageService.saveBizMaterials(entity);
		            jr.setResult(res);
		            log.debug("新增物资管理成功【{}】", entity);
		        } catch (Exception ex) {
		            log.error("新增物资管理出错", ex);
		            jr.setResult(ResultCode.ERROR);
		        }
    	 	}
        return jr;
    }

    /**
     * 删除物资管理
     * 
     * @param id 物资管理页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除物资管理")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable String id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除物资管理id={}",id);
            IResult codeMsg = bizmaterialsmanageService.deleteByIds(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除物资管理出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去修改物资管理
     * 
     * @param id 物资管理页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去修改物资管理")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "edit/{id}", method = { RequestMethod.GET})
    public ModelAndView updateForm(@PathVariable Long id, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            BizMaterialsManage entity = bizmaterialsmanageService.getById(id);
            mav.addObject("entity", entity);
            initCreateOrUpdateFrom(mav,"edit");
        } catch (Exception ex) {
            log.error("去修改物资管理", ex);
            mav.setView(new RedirectView("list"));
        }
        return mav;
    }

    /**
     * 修改物资管理
     * 
     * @param entity 物资管理页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改物资管理")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateBizMaterialsManage(BizMaterialsManage entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
      //判断物资来源是否存在
        IResult result = bizmaterialsmanageService.checkUpdateMaterials(entity);
    	String code = result.getCode();
    	 if ( code.equals(ResultCode.DUPLICATE_KEY.getCode())) {
        	 jr.setResult(result);
             log.debug("内部单位已存在【{}】", entity);
    	 }else {
		        try {
		            IResult res = bizmaterialsmanageService.updateMaterials(entity);
		            jr.setResult(res);
		            log.debug("修改物资管理成功【{}】", entity);
		        } catch (Exception ex) {
		            log.error("修改物资管理出错", ex);
		            jr.setResult(ResultCode.ERROR);
		        }
    	 	}
        return jr;
    }
    
    /**
     * 去查看物资管理
     * 
     * @param id 物资管理页面表单对象唯一标识
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-04-08]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看物资管理详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
    public ModelAndView info(@PathVariable Long id) {
        ModelAndView mav= new ModelAndView();
        try {
        	mav.addObject("materialsTypes", DicUtil.getLabelValueList(DicTypeEnum.MAT_TYPE));
            mav.addObject("units", DicUtil.getLabelValueList(DicTypeEnum.MAT_UNIT));
            mav.addObject("entity", bizmaterialsmanageService.getById(id));
            mav.setViewName(getView("bizmaterialsmanage_info"));
        } catch (Exception ex) {
            log.error("查看物资管理出错", ex);
        }
        return mav;
        
    }
    
}
